package com.fafu.control;

import com.fafu.model.Worker;
import com.fafu.pojo.AppPojo4list;
import com.fafu.pojo.WorkerHistoryPojo;
import com.fafu.service.WorkerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.Dictionary;
import util.JsonRsp;
import util.MyMap;
import util.StateChange;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.SUCCESS;

/**
 * 登陆接口使用user中的
 * 报修使用 repairColtrol中的
 */
@Api("用于管理人员android端接口")
@RestController()
@RequestMapping(value = "/appwork")
public class AppWorkControl {
    private Logger logger = LoggerFactory.getLogger(AppWorkControl.class);
    @Autowired
    private WorkerService workerService;
    @ApiOperation("维修师傅")
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public JsonRsp workerList(
            @RequestParam(value = "id")@ApiParam("id") Integer id){
        try{
            Worker worker ;
            worker = workerService.searchWorkerByid(id);
            worker.setPassword(null);
            worker.setSalt(null);
            return JsonRsp.success(worker);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("历史维修List")
    @RequestMapping(value = "/gethisList", method = RequestMethod.GET)
    public JsonRsp gethisList(
            @RequestParam(value = "id")@ApiParam("id") Integer id){
        try{
//            return JsonRsp.success(worker);
            List<WorkerHistoryPojo> reList ;
            reList= workerService.getHistorylist(id);
            for(WorkerHistoryPojo data:reList){
//                data.setAddTimeStr(TimeChange.Long2Date(data.getAddTime()));
//                System.out.println(data.getState());
                data.setStateStr(StateChange.getStateStr(data.getState()));

            }
            return JsonRsp.success(reList);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("进行中维修List")
    @RequestMapping(value = "/getrunList", method = RequestMethod.GET)
    public JsonRsp getrunList(
            @RequestParam(value = "workerId")@ApiParam("workerId") Integer workerId){
        Map<String,Object> map = workerService.selectRecordList(new MyMap().put("workerId",workerId).toMap());
        List<AppPojo4list> appPojo4listList = (List<AppPojo4list>) map.get("data");
        for(AppPojo4list data:appPojo4listList){
//                data.setAddTimeStr(TimeChange.Long2Date(data.getAddTime()));
//                System.out.println(data.getState());
            data.setStateStr(StateChange.getStateStr(data.getState()));

        }
        return  JsonRsp.success(map.get("data"));
    }


    @ApiOperation("注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public JsonRsp register(
            @RequestBody Worker worker){
        try{
            Map<String,Object> reMap = workerService.regiser(worker);
////            return JsonRsp.success(worker);
//            List<WorkerHistoryPojo> reList ;
//            reList= workerService.getHistorylist(id);
//            return JsonRsp.success(reList);
            if(reMap.get("code").equals(SUCCESS))
                return JsonRsp.success("success");
            return JsonRsp.fail((Integer) reMap.get("code"),(String)reMap.get("msg"));
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("师傅登录")
    @RequestMapping(value = "/worlogin", method=RequestMethod.GET)
    public JsonRsp stulogin(
            @RequestParam(value = "workerId") String workerId
            ,@RequestParam(value = "password") String password){
        try{
            Map<String,Object> map = workerService.login(new MyMap().put("workerId",workerId).put("password",password).toMap());
            if(map.get("code").equals(SUCCESS))
                return JsonRsp.success(new MyMap()
                        .put("iden",map.get("iden"))
                        .put("id",map.get("id"))
                        .put("isused",map.get("isused"))
                        .put("imgurl","null")
                        .put("name",map.get("name"))
                        .put("area",1)
                        .put("msg",map.get("msg")).toMap());
            else
                return JsonRsp.fail((Integer)map.get("code"),(String) map.get("msg"));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("密码修改")
    @RequestMapping(value = "/pwdchange", method=RequestMethod.GET)
    public JsonRsp pwdchange(
            @RequestParam(value = "workerId") String workerId
            , @RequestParam(value = "oldpwd") String oldpwd
            , @RequestParam(value = "newpwd") String newpwd){
        try{
            Map<String,Object> map = workerService.changePwd(new MyMap().put("workerId",workerId).put("oldpwd",oldpwd).put("newpwd",newpwd).toMap());
            if(map.get("code").equals(SUCCESS))
                return JsonRsp.success(new MyMap().put("msg","success").toMap());
            else
                return JsonRsp.fail((Integer)map.get("code"),(String) map.get("msg"));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @RequestMapping(value = "/searchItem",method = RequestMethod.GET)
    public JsonRsp searchItem(
            @RequestParam(value = "recordid") @ApiParam("记录id") Integer recordId){
        try {
            Map reMap ;
            reMap= workerService.selectRecordItem(new MyMap().put("id",recordId).toMap());
            return  JsonRsp.success(reMap.get("data"));
        } catch (Exception e) {
            logger.error("system error" + e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR, "system error");
        }
    }
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public JsonRsp search(
            @RequestParam(value = "id") @ApiParam("workerId") Integer workerId,
            @RequestParam(value = "type") @ApiParam("查询类别:0历史，1未读，2未处理, 3进行中，4拒绝") String type) {
        try {
            Map<String,Object> map = workerService.selectRecordList(new MyMap().put("workerId",workerId).put("type","(3)").toMap());
            List<AppPojo4list> appPojo4listList = (List<AppPojo4list>) map.get("data");
            for(AppPojo4list data:appPojo4listList){
//                data.setAddTimeStr(TimeChange.Long2Date(data.getAddTime()));
//                System.out.println(data.getState());
                data.setStateStr(StateChange.getStateStr(data.getState()));

            }
            return  JsonRsp.success(map.get("data"));
        } catch (Exception e) {
            logger.error("system error" + e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR, "system error");
        }
    }
    @ApiOperation("简介修改")
    @RequestMapping(value = "/msgchange", method=RequestMethod.GET)
    public JsonRsp pwdchange(
            @RequestParam(value = "workerId") Integer workerId
            , @RequestParam(value = "msg") String msg){
        try{
            Map<String,Object> map = workerService.msgChange(new MyMap().put("workerId",workerId).put("msg",msg).toMap());
            if(map.get("code").equals(SUCCESS))
                return  JsonRsp.success("success");
            return JsonRsp.fail((Integer) map.get("code"), (String) map.get("msg"));
        }catch (Exception e) {
            logger.error("system error" + e);
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR, "system error");
        }
    }
}
